package com.csgc.camera.widget.dialog

import android.content.Context
import android.widget.TextView
import androidx.core.content.ContextCompat

import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.widget.dialog.base.BottomDialog
import com.csgc.camera.util.etFilter.RemarkFilter
import com.cysd.spsyxj.R

import com.hjq.shape.layout.ShapeLinearLayout
import com.hjq.shape.layout.ShapeRelativeLayout
import com.hjq.shape.view.ShapeEditText
import com.hjq.shape.view.ShapeView
import com.hjq.toast.ToastUtils

class EditImageTxtDialog(val context: Context) {

    private lateinit var mDialog: BottomDialog
    private var inputText: ShapeEditText?=null
    private var colorView1: ShapeView?=null
    private var colorView2: ShapeView?=null
    private var colorView3: ShapeView?=null
    private var colorView4: ShapeView?=null
    private var colorView5: ShapeView?=null
    private var colorView6: ShapeView?=null
    private var colorView7: ShapeView?=null
    private var colorView8: ShapeView?=null
    private var colorView9: ShapeView?=null
    private var labelSure: TextView?=null
    private var modelStyle1: ShapeRelativeLayout?=null
    private var modelStyle2: ShapeLinearLayout?=null

    private var selectorColor = 0
    private var selectStyle = 0

    var listener:OnLabelCheckListener?=null

    init {
        initDialog()
    }
    private fun initDialog(){
        mDialog = BottomDialog(context).apply {
            setCustomView(R.layout.dialog_image_txt)
            inputText = findViewById(R.id.et_input_text)
            labelSure = findViewById(R.id.tv_sure_label)

            inputText?.filters = arrayOf(RemarkFilter(30))

            colorView1 = findViewById(R.id.sv_color_1)
            colorView2 = findViewById(R.id.sv_color_2)
            colorView3 = findViewById(R.id.sv_color_3)
            colorView4 = findViewById(R.id.sv_color_4)
            colorView5 = findViewById(R.id.sv_color_5)
            colorView6 = findViewById(R.id.sv_color_6)
            colorView7 = findViewById(R.id.sv_color_7)
            colorView8 = findViewById(R.id.sv_color_8)
            colorView9 = findViewById(R.id.sv_color_9)

            modelStyle1 = findViewById(R.id.layout_style_1)
            modelStyle2 = findViewById(R.id.layout_style_2)

            initEvents()
            changeColor(1)
        }

    }

    fun show(){
        inputText?.setText("")
        mDialog.show()
    }


    private fun initEvents(){



        labelSure?.clickWithTrigger {

            val text = inputText?.text?.toString()?.trim()
            if (text.isNullOrEmpty()){
                ToastUtils.show("请输入文字")
            }else if (selectStyle == 0){
                ToastUtils.show("请选择模版")
            }else{

                listener?.onCheck(selectStyle,getChangeColor(selectorColor),text)
            }
            mDialog.dismiss()
        }

        modelStyle1?.clickWithTrigger {
            changeStyle(1)
        }
        modelStyle2?.clickWithTrigger {
            changeStyle(2)
        }


        colorView1?.clickWithTrigger {changeColor(1) }
        colorView2?.clickWithTrigger { changeColor(2) }
        colorView3?.clickWithTrigger {changeColor(3)  }
        colorView4?.clickWithTrigger {changeColor(4)  }
        colorView5?.clickWithTrigger {changeColor(5)  }
        colorView6?.clickWithTrigger {changeColor(6)  }
        colorView7?.clickWithTrigger {changeColor(7)  }
        colorView8?.clickWithTrigger {changeColor(8)  }
        colorView9?.clickWithTrigger {changeColor(9)  }

    }

    private fun changeColor(active:Int){
        selectorColor = active
        colorView1?.isSelected = active == 1
        colorView2?.isSelected = active == 2
        colorView3?.isSelected = active == 3
        colorView4?.isSelected = active == 4
        colorView5?.isSelected = active == 5
        colorView6?.isSelected = active == 6
        colorView7?.isSelected = active == 7
        colorView8?.isSelected = active == 8
        colorView9?.isSelected = active == 9

    }

    interface OnLabelCheckListener{
        fun onCheck(style:Int,color:Int,text:String)
    }

    private fun changeStyle(style:Int){
        modelStyle1?.isSelected = style == 1
        modelStyle2?.isSelected = style == 2
        selectStyle = style
    }


    private fun getChangeColor(active:Int):Int{
       return if(active == 1){
           ContextCompat.getColor(context,R.color.white)
        }else if(active == 2){
           ContextCompat.getColor(context,R.color.black)
        }else if(active == 3){
           ContextCompat.getColor(context,R.color.color_FF0000)
        }else if(active == 4){
           ContextCompat.getColor(context,R.color.color_FF9900)
        }else if(active == 5){
           ContextCompat.getColor(context,R.color.color_FAFF00)
        }else if(active == 6){
           ContextCompat.getColor(context,R.color.color_00DE73)
        }else if(active == 7){
           ContextCompat.getColor(context,R.color.color_00D1FF)
        }else if(active == 8){
           ContextCompat.getColor(context,R.color.color_0057FF)
        }else if(active == 9){
           ContextCompat.getColor(context,R.color.color_7F00FF)
        }else{
           ContextCompat.getColor(context,R.color.white)
        }

    }

}